<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Khmerhr extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	function __construct(){
		parent:: __construct();
		$this->load->library('form_validation');
	}


	public function index()
	{
		$data["page"] = "home";
		$this->load->view("index",$data);
	}
	function news(){
		$data["page"] = "news";
		$this->load->view("index",$data);
	}
	function feedback(){
		$data["page"] = "feedback";
		$this->load->view("index",$data);
	}
	function help(){
		$data["page"] = "help";
		$this->load->view("index",$data);
	}
	function career(){
		$data["page"] = "career";
		$this->load->view("index",$data);
	}
	function about(){
		$data["page"] = "about";
		$this->load->view("index",$data);
	}
	function contact(){
		$data["page"] = "contact";
		$this->load->view("index",$data);
	}

	function service(){
		$data["page"] = "service";
		$this->load->view("index",$data);
	}

	function viewJobLocation($val){
		$arr = explode("_",$val);
		$data["page"] = "job";
		
		$data['jobView'] = $this->mod_khmerhr->geJobLocation($arr[1]);
		
		$this->load->view("index",$data);
	}
	function viewJobCateogry($val){
		$arr = explode("_",$val);
		$data["page"] = "job_cate";
		
		$data['jobView'] = $this->mod_khmerhr->geJobCategory($arr[1]);
		
		$this->load->view("index",$data);
	}
	
	function viewJobDetail($id){
		$data["page"] = "viewJobDetail";
		$arr = explode("_", $id);
		$data["result"] = $this->mod_khmerhr->getViewJobDetail($arr[1]);
		$this->load->view("index",$data);
	}

	function login(){
		if($this->session->userdata("logged_in")){
			redirect('khmerhr');

		}else{ 
			$data["page"] = "login";
			$this->form_validation->set_rules('password', 'Password', 'trim|required');
			$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
			if($this->form_validation->run() == FALSE){
				$this->load->view("index",$data);
			}else{
				$email = $this->input->post('email');
				$password = $this->input->post('password');
				$remember = $this->input->post('remember');
				$login = $this->mod_khmerhr->getLogin($email,MD5($password));
				if($login){
					$ses_arr = array(
						"userName"=>$login->first_name.' '.$login->last_name,
						"email"=>$login->email,
						"userId"=>$login->id,
						"userTypeId"=>$login->group_id,
						"logged_in"=>true
					);
					$this->session->set_userdata($ses_arr);
					redirect('khmerhr');
				}else{
					$this->session->set_flashdata("error_login","<span class='error_sms'>The email or password you entered is incorrect.</span>");
					redirect('khmerhr/login');
				}
			}
		}
	}
	function logout(){
		$this->session->sess_destroy();
		redirect('khmerhr');
	}
	function register(){
		if($this->session->userdata("logged_in")){
			redirect('khmerhr');
		}else{ 
			$data["page"] = "register";
			$this->form_validation->set_rules('firstName', 'First Name', 'trim|required');
			$this->form_validation->set_rules('lastName', 'Last Name', 'trim|required');
			$this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
			$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
			$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
			if($this->form_validation->run() == FALSE){
				$this->load->view("index",$data);
			}else{
				$firstName = $this->input->post('firstName');
				$lastName = $this->input->post('lastName');
				$email = $this->input->post('email');
				$password = $this->input->post('password');
				$checkMail = $this->mod_khmerhr->checkMailExist($email);
				if($checkMail > 0){
					$this->session->set_flashdata("message",'<div class="alert alert-warning alertText" role="alert">
						  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						  <strong>Warning! </strong> Email has been used.
						</div>' );
					redirect("khmerhr/register");
				}else{ 
					$result = $this->mod_khmerhr->register($firstName,$lastName,$email,$password);
					if($result){
						$this->session->set_flashdata("message",'<div class="alert alert-success alertText" role="alert">
						  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						  <strong>Well done! </strong> You successfully register.
						</div>' );
						redirect("khmerhr/register");
					}else{
						$this->session->set_flashdata("message",'<div class="alert alert-danger alertText" role="alert">
						  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						  <strong>Error! </strong> Please try again.
						</div>' );
						redirect("khmerhr/register");
					}
				}
			}
		}
		

		
	}


}

/* End of file welcome.php */
/* Location: ./application/controllers/khmerhr.php */